package ua.com.kangaroo.basic;

public class School {
	private int id;
	
	private String name;

	private String[] address;
	
	private String[] coordinator;
	
	private double earn = 0;
	
	private double debt = 0;
	
	private int[] uk = new int[12];
	
	private int[] ru = new int[12];
	
	private int[] en = new int[12];
	
	/**
	 * @author Oles Dobosevych
	 * @param id school id
	 * @param name school name
	 * @param coordinator school coordinator
	 * <li>coordinator[0] second name of the school coordinator</li>
	 * <li>coordinator[1] first name of the school coordinator</li>
	 * <li>coordinator[2] middle name of the school coordinator</li>
	 * @param address school address
	 * <li>address[0] school postCode</li>
	 * <li>address[1] school region</li>
	 * <li>address[2] school city</li>
	 * <li>address[3] school street</li>
	 * <li>address[4] school phoneCode</li>
	 * <li>address[5] school phone</li>
	 * <li>address[6] school fax</li>
	 * <li>address[7] school email</li>
	 */
	public School(int id, String name, String[] coordinator, String[] address) {
		this.id = id;
		this.name = name;
		
		String[] newCoordinator = new String[3];
		
		for (int i = 0; i < 3; i++) {
			newCoordinator[i] = "";
		}
		
		for (int i = 0; i < coordinator.length; i++) {
			if (coordinator[i] != null) {
				newCoordinator[i] = coordinator[i];
			}
		}
		
		this.coordinator = newCoordinator;
		
		String[] newAddress = new String[8];
		
		for (int i = 0; i < 8; i++) {
			newAddress[i] = "";
		}
		
		for(int i = 0; i < address.length; i++) {
			if (address[i] != null) {
				newAddress[i] = address[i];
			}
		}
		
		this.address = newAddress;
		
		for (int i = 0; i < 12; i++) {
			uk[i] = 0;
			ru[i] = 0;
			en[i] = 0;
		}
	}
	
	/**
	 * @return school debt
	 */
	public double getDebt() {
		return debt;
	}
	
	/**
	 * @return school earn
	 */
	public double getEarn() {
		return earn;
	}
	
	/**
	 * @param debt school debt
	 */
	public void setDebt(double debt) {
		this.debt = debt;
	}
	
	/**
	 * @param earn school earn
	 */
	public void setEarn(double earn) {
		this.earn = earn;
	}
	
	/**
	 * @return school id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @return school name
	 */
	public String getName() {
		return name;
	}
	
	/**
	 * @param name school name
	 */
	public void setName(String name) {
		this.name = name;
	}
	
	/**
	 * @param school address
	 */
	public void setAddress(String[] address) {
		this.address = address;
	}
	
	/**
	 * @param school coordinator
	 */
	public void setCoordinator(String[] coordinator) {
		this.coordinator = coordinator;
	}
	
	/**
	 * @return school address
	 */
	public String[] getAddress() {
		return address;
	}
	
	/**
	 * @return school coordinator
	 */
	public String[] getCoordinator() {
		return coordinator;
	}
	
	/**
	 * @return number of participants who chose problems in English language
	 */
	public int[] getEn() {
		return en;
	}
	
	/**
	 * @return number of participants who chose problems in Russian language
	 */
	public int[] getRu() {
		return ru;
	}
	
	/**
	 * @return number of participants who chose problems in Ukrainian language
	 */
	public int[] getUk() {
		return uk;
	}
	
	/**
	 * @param uk number of participants who chose problems in Ukrainian language
	 */
	public void setUk(int[] uk) {
		this.uk = uk;
	}
	
	/**
	 * @param ru number of participants who chose problems in Russian language
	 */
	public void setRu(int[] ru) {
		this.ru = ru;
	}
	
	/**
	 * @param en number of participants who chose problems in English language
	 */
	public void setEn(int[] en) {
		this.en = en;
	}

	/**
	 * @return territory name of the school
	 */
	public String getTerritory() {
		return SchoolsData.getTerritory(id);
	}
	
	/**
	 * @return school id as String in five-digits format
	 */
	public String idToString() {
		return SchoolsData.idToString(id);
	}
	
	/**
	 * @return school full name
	 */
	public String getFullName() {
		StringBuilder sb = new StringBuilder();
		sb.append(name);
		sb.append(", ");
		sb.append(address[2]);
		sb.append(", ");
		sb.append(address[1]);
		return sb.toString();
	}
	
	/**
	 * @param level kangaroo level, can be equal to "maluk2", "maluk34", "shkolyaryk", "cadet", "junior", "vypusknyk", "all"
	 * @param language of the problem, can be equal to "uk", "ru", "en", "all"
	 * @return number of the participants who chose specified level and specified language 
	 */
	public int getNumberOfParticipants(String level, String language){
		if (level.equals("maluk2") && language.equals("uk")) {
			return uk[2];
		}
		if (level.equals("maluk2") && language.equals("ru")) {
			return ru[2];
		}
		if (level.equals("maluk2") && language.equals("en")) {
			return en[2];
		}
		if (level.equals("maluk2") && language.equals("all")) {
			return uk[2] + ru[2] + en[2];
		}
		if (level.equals("maluk34") && language.equals("uk")) {
			return uk[3] + uk[4];
		}
		if (level.equals("maluk34") && language.equals("ru")) {
			return ru[3] + ru[4];
		}
		if (level.equals("maluk34") && language.equals("en")) {
			return en[3] + en[4];
		}
		if (level.equals("maluk34") && language.equals("all")) {
			return uk[3] + uk[4] + ru[3] + ru[4] + en[3] + en[4];
		}
		if (level.equals("shkolyaryk") && language.equals("uk")) {
			return uk[5] + uk[6];
		}
		if (level.equals("shkolyaryk") && language.equals("ru")) {
			return ru[5] + ru[6];
		}
		if (level.equals("shkolyaryk") && language.equals("en")) {
			return en[5] + en[6];
		}
		if (level.equals("shkolyaryk") && language.equals("all")) {
			return uk[5] + uk[6] + ru[5] + ru[6] + en[5] + en[6];
		}
		if (level.equals("cadet") && language.equals("uk")) {
			return uk[7] + uk[8];
		}
		if (level.equals("cadet") && language.equals("ru")) {
			return ru[7] + ru[8];
		}
		if (level.equals("cadet") && language.equals("en")) {
			return en[7] + en[8];
		}
		if (level.equals("cadet") && language.equals("all")) {
			return uk[7] + uk[8] + ru[7] + ru[8] + en[7] + en[8];
		}
		if (level.equals("junior") && language.equals("uk")) {
			return uk[9] + uk[10];
		}
		if (level.equals("junior") && language.equals("ru")) {
			return ru[9] + ru[10];
		}
		if (level.equals("junior") && language.equals("en")) {
			return en[9] + en[10];
		}
		if (level.equals("junior") && language.equals("all")) {
			return uk[9] + uk[10] + ru[9] + ru[10] + en[9] + en[10];
		}
		if (level.equals("vypusknyk") && language.equals("uk") ){
			return uk[11];
		}
		if (level.equals("vypusknyk") && language.equals("ru")) {
			return ru[11];
		}
		if (level.equals("vypusknyk") && language.equals("en")) {
			return en[11];
		}
		if (level.equals("vypusknyk") && language.equals("all")) {
			return uk[11] + ru[11] + en[11];
		}
		if (level.equals("all") && language.equals("uk")) {
			int num = 0;
			for (int i = 2; i < 12; i++) {
				num = num + uk[i];
			}
		}
		if (level.equals("all") && language.equals("ru")) {
			int num = 0;
			for (int i = 2; i < 12; i++) {
				num = num + ru[i];
			}
		}
		if (level.equals("all") && language.equals("en")) {
			int num = 0;
			for (int i = 2; i < 12; i++) {
				num = num + en[i];
			}
		}
		if (level.equals("all") && language.equals("all")) {
			int num = 0;
			for (int i = 2; i < 12; i++) {
				num = num + uk[i];
			}
			for (int i = 2; i < 12; i++) {
				num = num + ru[i];
			}
			for (int i = 2; i < 12; i++) {
				num = num + en[i];
			}
			return num;
		}
		return 0;
	}
	
	/**
	 * @return school to address.dat format
	 */
	public String toAddress(){
		StringBuilder sb = new StringBuilder();
		sb.append(idToString());
		sb.append("\r\n");
		for (int i = 0; i < 3; i++){
			sb.append(coordinator[i]);
			sb.append("|");
		}
		sb.append(address[0]);
		sb.append("|");
		sb.append(getTerritory());
		sb.append("|");
		for (int i = 1; i < 8; i++){
			sb.append(address[i]);
			sb.append("|");
		}
		sb.append(name);
		return sb.toString();
	}
	
	/**
	 * @param arrayOfInt array of participants by forms 
	 * @return array of participants by forms in String 
	 */
	private String arrayToString(int[] arrayOfInt){
		StringBuilder sb = new StringBuilder();
		sb.append(Integer.toString(arrayOfInt[2]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[3]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[4]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[3] + arrayOfInt[4]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[5]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[6]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[5] + arrayOfInt[6]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[7]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[8]));
		sb.append(",");
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[7] + arrayOfInt[8]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[9]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[10]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[9] + arrayOfInt[10]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[11]));
		sb.append(",");
		sb.append(Integer.toString(arrayOfInt[11]));
		return sb.toString();
	}
	
	/**
	 * @return school to address.dat format
	 */
	public String toBase(){
		StringBuilder sb = new StringBuilder();
		sb.append(idToString());
		sb.append("\r\n");
		sb.append(arrayToString(uk));
		sb.append(",");
		sb.append(",");
		sb.append(arrayToString(ru));
		sb.append(",");
		sb.append(",");
		sb.append(Double.toString(earn));
		sb.append(",");
		sb.append(Double.toString(debt));
		sb.append(",");
		sb.append(",");
		sb.append(arrayToString(en));
		return sb.toString();
	}
}
